package com.xiebishe.mylibrary.common.groupAdapter;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Looper;


import com.xiebishe.mylibrary.base.MyApplication;

import java.util.List;

public class ThreadUtils {
    private ThreadUtils() {
    }

    public static void AssertMainThread(String methodName) {
        if (!isMainThread()) {
            throw new IllegalStateException("Cannot invoke " + methodName + " on a background"
                    + " thread");
        }
    }

    public static boolean isMainThread() {
        return Looper.getMainLooper().getThread() == Thread.currentThread();
    }


    public static String getProcessName() {
        ActivityManager am = (ActivityManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return "";
        }
        for (ActivityManager.RunningAppProcessInfo proInfo : runningApps) {
            if (proInfo.pid == android.os.Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName;
                }
            }
        }
        return "";
    }
}
